package com.umgsai.wx.backend.test;

public class MaxProfit {

    /**
     * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
     */
    public static void main(String[] args) {
        int[] a = new int[]{7, 1, 5, 3, 6, 4};
//        int[] a = new int[]{7, 6, 4, 3, 1};
        int maxProfit = getMaxProfit(a);
        System.out.println(maxProfit);
    }


    public static int getMaxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int price : prices) {
            if (price < minPrice) {
                minPrice = price;
            }
            if (price - minPrice > maxProfit) {
                maxProfit = price - minPrice;
            }
        }
        return maxProfit;
    }
}
